<template>
  <view>
    <nav-bar title="选择地址" backgroundColor="#115BAA" titleColor="#FFF"></nav-bar>
    <you-scroll
      :loadingHeight="loadingHeight"
      :mainHeight="scrollHeight"
      :isAllData="isAllData"
      :isRequestSuccess="isRequestSuccess"
      outPaddingTop="20rpx"
      @onRefresh="onRefresh"
      @loadMore="loadMore"
    >
      <template>
        <view v-if="isLoading" class="common-loading">
          <image class="loading-img" :src="baseIconUrl + '/commonIcon/icon_loading.png'" />
          正在加载数据，请稍后......
        </view>
        <view class="common-no-data" v-else-if="!addressList || addressList.length === 0">
          <image class="no-data-img" :src="baseIconUrl + '/commonIcon/icon_no_data.png'" />
          <view class="text">暂无数据</view>
        </view>
        <view v-else>
          <view
            class="address-item"
            v-for="(item, index) in addressList"
            :key="index"
            @click="clickAddressItem(item)"
          >
            <view class="company">{{ item.companyName || item.contactPer }}</view>
            <view class="person-box">
              <view class="person">{{ item.receiverName }}</view>
              <view class="phone">{{ item.phoneNumber }}</view>
            </view>
            <view class="address">{{ item.address }}</view>
            <view
              :class="[
                'choose-status',
                chooseAddress.id && item.id === chooseAddress.id ? 'active-status' : ''
              ]"
            >
              {{ chooseAddress.id && item.id === chooseAddress.id ? '当前选择' : '选择' }}
            </view>
          </view>
        </view>
      </template>
    </you-scroll>
  </view>
</template>

<script>
  import api_user from '@/api/user'
  import { baseIconUrl } from '@/config'
  export default {
    name: 'ChooseAddress',
    data() {
      return {
        baseIconUrl,
        loadingHeight: '',
        scrollHeight: '',
        isAllData: false,
        isRequestSuccess: false,
        isLoading: false,
        addressList: [],
        pageNum: 1,
        pageSize: 10,
        chooseAddress: {}
      }
    },
    onLoad(options) {
      this.scrollHeight = this.$store.getters.heightData.noTabMainHeight + 'px'
      this.loadingHeight = this.$store.getters.heightData.noTabMainHeight - 20 + 'px'
      if (options.chooseAddress) {
        this.chooseAddress = JSON.parse(decodeURIComponent(options.chooseAddress))
      }
      this.getAddressList()
    },
    methods: {
      getAddressList() {
        const params = {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
        api_user.fetchPickupList(params).then(res => {
          if (res.code !== 200) return
          this.addressList.push(...res.rows)
          this.isRequestSuccess = true
          this.total = res.total
        })
      },
      clickAddressItem(item) {
        const chooseAddress = encodeURIComponent(JSON.stringify(item))
        uni.redirectTo({
          url: `/subPages/userIndex/rehabilitationAids/orderConfirm?chooseAddress=${chooseAddress}&tabActive=selfPickup`
        })
      },
      // 下拉刷新
      onRefresh() {
        this.pageNum = 1
        this.addressList = []
        this.isAllData = false
        this.isRequestSuccess = false
        this.getAddressList()
      },
      // 上拉加载
      loadMore() {
        if (this.addressList.length < this.total) {
          this.pageNum++
          this.isAllData = false
          this.isRequestSuccess = false
          this.getAddressList()
        } else {
          this.isAllData = true
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .address-item {
    position: relative;
    margin: 0 30rpx 20rpx;
    padding: 36rpx 24rpx;
    border-radius: 20rpx;
    background-color: #FFF;
    .company {
      font-size: 28rpx;
      font-weight: 500;
      color: #333;
    }
    .person-box {
      display: flex;
      align-items: center;
      margin: 14rpx 0;
      font-size: 28rpx;
      .person {
        margin-right: 30rpx;
      }
    }
    .address {
      font-size: 24rpx;
      color: #999;
    }
    .choose-status {
      position: absolute;
      top: 0;
      right: 0;
      width: 140rpx;
      height: 48rpx;
      border-radius: 0rpx 20rpx 0rpx 20rpx;
      text-align: center;
      line-height: 48rpx;
      font-size: 24rpx;
      color: #2A82E4;
      background-color: #d4e6fa;
    }
    .active-status {
      color: #FFF;
      background-color: #2A82E4;
    }
  }
</style>
